package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {

  public static void main(String[] args) {
    ExecutorService threadPool = Executors.newFixedThreadPool(12);
    final int[] array = new int[20];
    for(int i = 0; i < array.length; i++) {
      array[i] = i;
    }
    
    
    for(int i = 0; i < array.length; i++) {
      
      final int a = i;
      threadPool.execute(new Runnable() {
        @Override
        public void run() {
          System.out.println(Thread.currentThread().getName() + " " + array[a]);
        }
      });
    }
    System.out.println("开始");
    threadPool.shutdown();
    System.out.println("结束");
  }
  
  public static void main2(String[] args) {

    Runnable task = new Runnable() {

      @Override
      public void run() {
        System.out.println("nihao");
      }
    };

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    executor.scheduleWithFixedDelay(task, 0, 100, TimeUnit.MILLISECONDS);
    
    executor.shutdown();
  }

}
